package minh.bac.wcbrazil2014.homnay;

import java.util.Calendar;

import minh.bac.wcbrazil2014.R;
import minh.bac.wcbrazil2014.WCTAG;
import minh.bac.wcbrazil2014.countdown.CountDownFragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class HomNayFragment extends Fragment {
	/*
	 * chao anh bac trong nay anh em minh se hien thi lich dau trong ngay hien
	 * tai ma ho mo app thi trong nay em se tao san fragmentmanager cho anh su
	 * dung do fragment se nam trong viewpager nen em se goi fragment manager
	 * kha mot chut neu nhu ko co bug gi xay ra thi ngon con co thi sua thoi
	 */

	static FragmentManager fragmentManager;
	public static HomNayFragment newInstance()
	{
		HomNayFragment fragment = new HomNayFragment();
		return fragment;
	}

	CountDownFragment countDownFragment;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View rootView = inflater.inflate(R.layout.homnay_layout, container,
				false);
		// -------------------------------------------------------------------------------------------------------------------------
		// goi fragment manager
		// ------------------------------------------------------------------------------------------------------------------------
		fragmentManager = this.getChildFragmentManager();
		//
		Calendar calendar = Calendar.getInstance();
		int month = calendar.get(Calendar.MONTH)+1;
		int day  = calendar.get(Calendar.DATE);
		Log.d("TIME", month+"" + "" + day);
		if(month >= 6 && day > 12 )
		{
			switchFragment(R.id.container_homnay, ListViewHomnayFM.newInstance(), true, WCTAG.TAGFM_LISTVIEWHOMNAY);
			
		}
		else
		{
			CountDownFragment countDownFragment = new CountDownFragment();
			switchFragment(R.id.container_homnay, countDownFragment, false, WCTAG.TAGFM_COUNTDOWN);
		}
		
		return rootView;
	}

	/**
	 * day la phuong thuc em viet san de anh add fragment vao de hon
	 * 
	 * @param idContainer
	 *            la id cua framelayout container anh can truyen vao vi du
	 *            R.id.container_homnay
	 * @param fragment
	 *            fragment anh muon truyen vao vd : new Fragment countdown
	 * @param addToBackStack
	 *            la anh co muon add vao backstack hay khong khi nhan phim back
	 *            se quay lai
	 * @param TAG
	 *            tag cua fragment neu nhu ko co addtobackstacg thi khong can
	 *            thiet con co addbackstack thi anh nen vao TAGWC de them tag
	 *            cua fragment
	 */
	public static void switchFragment(int idContainer, Fragment fragment,
			boolean addToBackStack, String TAG) {
		if (addToBackStack) {
			if (TAG != null) {
				fragmentManager.beginTransaction()
						.replace(idContainer, fragment, TAG)
						.addToBackStack(TAG).commit();
			} else {
				fragmentManager.beginTransaction()
						.replace(idContainer, fragment).addToBackStack(null)
						.commit();
			}
		} else {
			if (TAG != null) {
				fragmentManager.beginTransaction()
						.replace(idContainer, fragment, TAG).commit();
			} else {
				fragmentManager.beginTransaction()
						.replace(idContainer, fragment).commit();
			}
		}
	}
}
